<template>
  <header class="navbar" :class="{ 'navbar-scrolled': isScrolled }">
    <div class="container navbar-container">
      <router-link to="/" class="logo">
        <span class="logo-text">南有乔木不可休思</span>
      </router-link>
      
      <nav class="nav-links" :class="{ 'nav-open': isMenuOpen }" @click.self="closeMenu">
        <router-link 
          v-for="item in navItems" 
          :key="item.path"
          :to="item.path"
          class="nav-link"
          @click.native="closeMenu"
        >
          {{ $t(item.title) }}
        </router-link>
      </nav>
      
      <div class="controls">
        <div class="theme-switcher">
          <button 
            v-for="theme in themes" 
            :key="theme.value"
            class="theme-btn"
            :class="{ active: currentTheme === theme.value }"
            @click="changeTheme(theme.value)"
          >
            <i :class="theme.icon"></i>
            <span class="tooltip">{{ theme.label }}</span>
          </button>
        </div>
        
        <div class="lang-switcher">
          <button 
            v-for="lang in languages" 
            :key="lang.value"
            class="lang-btn"
            :class="{ active: currentLang === lang.value }"
            @click="changeLang(lang.value)"
          >
            {{ lang.label }}
          </button>
        </div>
        
        <button 
          class="menu-btn"
          :class="{ active: isMenuOpen }"
          @click="toggleMenu"
          aria-label="菜单"
        >
          <div class="menu-icon">
            <span></span>
          </div>
        </button>
      </div>
    </div>
  </header>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'TheNavbar',
  
  data() {
    return {
      isScrolled: false,
      isMenuOpen: false,
      currentLang: this.$i18n.locale,
      currentTheme: localStorage.getItem('theme') || 'system',
      navItems: [
        { path: '/', title: 'nav.home' },
        { path: '/about', title: 'nav.about' },
        { path: '/projects', title: 'nav.projects' },
        { path: '/contact', title: 'nav.contact' }
      ],
      themes: [
        { value: 'light', label: '浅色', icon: 'fas fa-sun' },
        { value: 'dark', label: '深色', icon: 'fas fa-moon' },
        { value: 'system', label: '跟随系统', icon: 'fas fa-desktop' }
      ],
      languages: [
        { value: 'zh', label: '中' },
        { value: 'en', label: 'En' }
      ]
    }
  },
  
  computed: {
    ...mapState(['theme'])
  },
  
  methods: {
    ...mapMutations(['setTheme', 'setLanguage']),
    
    handleScroll() {
      this.isScrolled = window.scrollY > 50
    },
    
    changeTheme(theme) {
      if (theme === 'system') {
        const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches
        this.setTheme(isDark ? 'dark' : 'light')
      } else {
        this.setTheme(theme)
      }
      localStorage.setItem('theme', theme)
      this.currentTheme = theme
    },
    
    toggleMenu() {
      this.isMenuOpen = !this.isMenuOpen
      document.body.style.overflow = this.isMenuOpen ? 'hidden' : ''
    },
    
    changeLang(lang) {
      this.setLanguage(lang)
      this.$i18n.locale = lang
      this.currentLang = lang
    },
    
    closeMenu() {
      this.isMenuOpen = false
      document.body.style.overflow = ''
    }
  },
  
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
    document.body.style.overflow = ''
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: var(--header-height);
  background-color: var(--bg-primary);
  transition: background-color var(--transition-speed),
              box-shadow var(--transition-speed);
  z-index: 100;
  
  &.navbar-scrolled {
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  @media (max-width: 768px) {
    .nav-links {
      position: fixed;
      top: var(--header-height);
      left: 0;
      width: 100%;
      height: calc(100vh - var(--header-height));
      background-color: var(--bg-primary);
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transform: translateX(-100%);
      transition: transform var(--transition-speed);
      z-index: 1000;
      
      &.nav-open {
        transform: translateX(0);
      }
      
      .nav-link {
        font-size: 1.2rem;
        margin: 1rem 0;
        padding: 0.5rem 1rem;
      }
    }
  }
}

.navbar-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
}

.logo {
  text-decoration: none;
  color: var(--accent-color);
  font-weight: bold;
  font-size: 1.2rem;
  
  .logo-text {
    transition: color var(--transition-speed);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 200px;
    display: block;
  }
}

.nav-links {
  display: flex;
  gap: 2rem;
  
  .nav-link {
    color: var(--text-primary);
    text-decoration: none;
    font-weight: 500;
    transition: color var(--transition-speed);
    
    &:hover,
    &.router-link-active {
      color: var(--accent-color);
    }
  }
}

.controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  
  .theme-switcher {
    display: flex;
    gap: 0.5rem;
    background: var(--bg-secondary);
    padding: 0.25rem;
    border-radius: var(--border-radius);
  }
  
  .theme-btn {
    position: relative;
    width: 2rem;
    height: 2rem;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-speed);
    display: flex;
    align-items: center;
    justify-content: center;
    
    &:hover {
      color: var(--accent-color);
      
      .tooltip {
        opacity: 1;
        transform: translateY(0);
      }
    }
    
    &.active {
      background: var(--accent-color);
      color: white;
    }
    
    .tooltip {
      position: absolute;
      bottom: -2rem;
      left: 50%;
      transform: translateX(-50%) translateY(0.5rem);
      padding: 0.25rem 0.5rem;
      background: var(--bg-primary);
      color: var(--text-primary);
      font-size: 0.75rem;
      border-radius: 0.25rem;
      white-space: nowrap;
      opacity: 0;
      transition: all var(--transition-speed);
      pointer-events: none;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    }
  }
  
  .lang-switcher {
    display: flex;
    background: var(--bg-secondary);
    padding: 0.25rem;
    border-radius: var(--border-radius);
    
    .lang-btn {
      width: 2rem;
      height: 2rem;
      border: none;
      background: transparent;
      color: var(--text-secondary);
      cursor: pointer;
      border-radius: var(--border-radius);
      transition: all var(--transition-speed);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.875rem;
      
      &:hover {
        color: var(--accent-color);
      }
      
      &.active {
        background: var(--accent-color);
        color: white;
      }
    }
  }
}

.menu-btn {
  display: none;
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  transition: all var(--transition-speed);
  margin-left: 0.5rem;
  position: relative;
  align-items: center;
  justify-content: center;
  
  .menu-icon {
    position: absolute;
    width: 22px;
    height: 2px;
    
    span,
    span::before,
    span::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 2px;
      background-color: var(--text-primary);
      border-radius: 2px;
      transition: all 0.3s ease;
    }
    
    span {
      position: absolute;
      transform: translateY(-50%);
      width: 100%;
    }
    
    span::before {
      transform: translateY(-7px);
    }
    
    span::after {
      transform: translateY(7px);
    }
  }
  
  &:hover {
    .menu-icon {
      span,
      span::before,
      span::after {
        background-color: var(--accent-color);
      }
    }
  }
  
  &.active {
    .menu-icon {
      span {
        background-color: transparent;
      }
      
      span::before {
        transform: translateY(0) rotate(45deg);
        background-color: var(--accent-color);
      }
      
      span::after {
        transform: translateY(0) rotate(-45deg);
        background-color: var(--accent-color);
      }
    }
  }
  
  &:active {
    transform: scale(0.95);
  }
}

@media (max-width: 768px) {
  .menu-btn {
    display: flex;
  }
  
  .nav-links {
    position: fixed;
    top: var(--header-height);
    left: 0;
    width: 100%;
    height: calc(100vh - var(--header-height));
    background-color: var(--bg-primary);
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transform: translateX(-100%);
    transition: transform var(--transition-speed);
    z-index: 1000;
    
    &.nav-open {
      transform: translateX(0);
    }
    
    .nav-link {
      font-size: 1.2rem;
      margin: 1rem 0;
      padding: 0.5rem 1rem;
    }
  }
  
  .logo {
    font-size: 1rem;
    
    .logo-text {
      max-width: 120px;
    }
  }
  
  .controls {
    gap: 0.5rem;
    margin-left: auto;
    
    .theme-switcher,
    .lang-switcher {
      background: transparent;
      padding: 0;
    }
    
    .theme-btn,
    .lang-btn {
      width: 2rem;
      height: 2rem;
      font-size: 0.8rem;
      padding: 0;
      margin: 0;
      
      &.active {
        background: var(--accent-color);
        color: white;
      }
      
      &:hover {
        background: transparent;
        color: var(--accent-color);
      }
    }
    
    .lang-switcher {
      margin-right: 0.5rem;
      
      .lang-btn {
        width: 1.8rem;
        height: 1.8rem;
        font-weight: 500;
        
        &.active {
          background: var(--accent-color);
          color: white;
        }
      }
    }
    
    .tooltip {
      display: none;
    }
  }
  
  .menu-btn {
    margin-left: 0;
  }
}

@media (max-width: 480px) {
  .logo {
    .logo-text {
      max-width: 100px;
    }
  }
}
</style> 